//
//  ZLPSaveTool.m
//  TangYi
//
//  Created by dllo on 15/11/2.
//  Copyright © 2015年 赵连鹏. All rights reserved.
//

#import "ZLPSaveTool.h"

@implementation ZLPSaveTool

+ (void)isHaveFolder {
    // 找沙盒路径
   
    NSString *sandBoxPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
    // 拼接文件夹名
    NSString *DicFile = [sandBoxPath stringByAppendingPathComponent:@"Dic"];
    // 创建文件管理者
    NSFileManager *manager = [NSFileManager defaultManager];
    // 通过管理者调用方法 根据路径判断文件夹是否存在
    if (![manager fileExistsAtPath:DicFile]) {
        // 创建文件夹
        [manager createDirectoryAtPath:DicFile withIntermediateDirectories:YES attributes:nil error:nil];
    }
    NSLog(@"%@", DicFile);
}

+ (BOOL)isHaveDicInPlist:(NSDictionary *)dic {
    NSString *sandBoxPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)lastObject];
    NSString *docPath = [sandBoxPath stringByAppendingPathComponent:@"Dic/dic.plist"];
    // 反归档
    NSArray *DicArr = [NSKeyedUnarchiver unarchiveObjectWithFile:docPath];
    // 遍历
    for (NSDictionary *temp in DicArr) {
//        if ([temp.numID isEqualToString:topic.numID]) {
//            return NO;
//        }
        if (temp == dic) {
            return NO;
        }
    }
    return YES;
}

+ (void)saveDicToPlist:(NSDictionary *)dic {
    NSString *sandBoxPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
    NSString *docPath = [sandBoxPath stringByAppendingPathComponent:@"Dic/dic.plist"];
    NSMutableArray *dicArr = [NSKeyedUnarchiver unarchiveObjectWithFile:docPath];
    if (dicArr == nil) {
        dicArr = [NSMutableArray array];
    }
    [dicArr addObject:dic];
    // 归档
    [NSKeyedArchiver archiveRootObject:dicArr toFile:docPath];
    NSLog(@"%@", docPath);
}

+ (NSMutableArray *)selectDicInPlist {
    
    NSString *sandBoxPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)lastObject];
    NSString *docPath = [sandBoxPath stringByAppendingPathComponent:@"Dic/dic.plist"];
    // 反归档
    NSMutableArray *DicArr = [NSKeyedUnarchiver unarchiveObjectWithFile:docPath];
    
    
    
    return DicArr;

    
}

+ (void)cancelDicInPlist:(NSDictionary *)topic {
    NSString *sandBoxPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
    NSString *docPath = [sandBoxPath stringByAppendingPathComponent:@"Dic/dic.plist"];
    NSMutableArray *topicArr = [NSKeyedUnarchiver unarchiveObjectWithFile:docPath];
//    for (ZLPTableViewModel *temp in topicArr) {
//        if ([temp.numID isEqualToString:topic.numID]) {
//            [topicArr removeObject:temp];
//            break;
//        }
//    }
    [NSKeyedArchiver archiveRootObject:topicArr toFile:docPath];
}

@end
